package com.hyz.advice;

import com.hyz.model.ResponseResult;
import com.hyz.model.asset.Asset;
import com.hyz.model.asset.AssetStatus;
import com.hyz.model.assetMaintenance.AssetMaintenance;
import com.hyz.model.assetMaintenance.AssetMaintenanceResult;
import com.hyz.model.assetMaintenance.AssetMaintenanceStatus;
import com.hyz.model.assetRetirement.AssetRetirement;
import com.hyz.model.assetRetirement.AssetRetirementResult;
import com.hyz.model.assetRetirement.AssetRetirementStatus;
import com.hyz.service.AssetMaintenanceService;
import com.hyz.service.AssetRetirementService;
import com.hyz.service.AssetService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author HGD
 * @date 2022/10/19 15:04
 */
@Component
@Aspect
public class ControllerAdvice {
    @Autowired
    private AssetRetirementService arService;
    @Autowired
    private AssetService assetService;

    //-------------------------------------------------------------------------
    // 资产报废

    @Pointcut("execution(* com.hyz.controller.*.*UpdateAssetRetirement(..))")
    private void ifAssetRetirementResultIsFailed() {
    }

    /**
     * 获取所有资产报废的审批申请。如果资产借调申请是失败则将资产装填更新
     *
     * @param pjp pjp
     * @throws Throwable 切入点运行异常
     */
    @Around("ifAssetRetirementResultIsFailed()")
    private Object setAssetStatusByAssetRetirementResul(ProceedingJoinPoint pjp) throws Throwable {
        ResponseResult<Boolean> proceed = (ResponseResult<Boolean>) pjp.proceed();
        Object[] args = pjp.getArgs();
        for (Object arg : args) {
            if (arg.getClass().equals(AssetRetirementResult.class) && proceed.getData()) {
                AssetRetirementResult result = (AssetRetirementResult) arg;
                if (Objects.equals(result, AssetRetirementResult.FAILED)) {
                    Long arId = (Long) args[0];
                    AssetRetirement ar = arService.getById(arId);
                    AssetRetirementStatus status = ar.getStatus();
                    Asset asset = assetService.getById(ar.getAssetId());
                    if (Objects.equals(status, AssetRetirementStatus.IN_REPOSITORY)) {
                        asset.setStatus(AssetStatus.IN_REPOSITORY);
                    } else if (Objects.equals(status, AssetRetirementStatus.SECONDMENT)) {
                        asset.setStatus(AssetStatus.SECONDMENT);
                    }
                    assetService.saveOrUpdate(asset);
                }
            }
        }
        return proceed;
    }

    //-------------------------------------------------------------------------
    // 资产维修

    @Autowired
    private AssetMaintenanceService amService;

    @Pointcut("execution(* com.hyz.controller.*.*UpdateAssetMaintenance(..))")
    private void ifAssetMaintenanceResultIsFailed() {
    }

    @Around("ifAssetMaintenanceResultIsFailed()")
    private Object setAssetStatusByAssetMaintenance(ProceedingJoinPoint pjp) throws Throwable {
        ResponseResult<Boolean> proceed = (ResponseResult<Boolean>) pjp.proceed();
        Object[] args = pjp.getArgs();
        for (Object arg : args) {
            if (arg.getClass().equals(AssetMaintenanceResult.class) && proceed.getData()) {
                AssetMaintenanceResult result = (AssetMaintenanceResult) arg;
                boolean amResult = Objects.equals(result, AssetMaintenanceResult.OPERATIONS_SUCCESS)
                        || Objects.equals(result, AssetMaintenanceResult.ASSET_MANAGER_FAILED)
                        || Objects.equals(result, AssetMaintenanceResult.FAILED);
                if (amResult) {
                    Long arId = (Long) args[0];
                    AssetMaintenance am = amService.getById(arId);
                    AssetMaintenanceStatus status = am.getStatus();
                    Asset asset = assetService.getById(am.getAssetId());
                    if (Objects.equals(status, AssetMaintenanceStatus.IN_REPOSITORY)) {
                        asset.setStatus(AssetStatus.IN_REPOSITORY);
                    } else if (Objects.equals(status, AssetMaintenanceStatus.SECONDMENT)) {
                        asset.setStatus(AssetStatus.SECONDMENT);
                    }
                    assetService.saveOrUpdate(asset);
                }
            }
        }
        return proceed;
    }
}
